package org.ehe.erp.controller.finance;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.erp.domain.finance.bo.ErpFinancePaymentBo;
import org.ehe.erp.domain.finance.vo.ErpFinancePaymentVo;
import org.ehe.erp.service.finance.ErpFinancePaymentService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * ERP付款单管理器
 * @author :zhangnn
 * @className :ErpFinancePaymentController
 * @description: 付款单管理功能
 * @date 2025-08-18 14:32:41
 */
@Slf4j
@RequestMapping("/erp/payment")
@RestController
@RequiredArgsConstructor
public class ErpFinancePaymentController extends BaseController {
    private final ErpFinancePaymentService paymentService;

    /**
     * 查询ERP付款单列表
     */
    @SaCheckPermission("erp:payment:list")
    @GetMapping("/list")
    public TableDataInfo<ErpFinancePaymentVo> list(ErpFinancePaymentBo bo, PageQuery pageQuery) {
        return paymentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出ERP付款单列表
     */
    @SaCheckPermission("erp:payment:export")
    @Log(title = "ERP付款单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpFinancePaymentBo bo, HttpServletResponse response) {
        List<ErpFinancePaymentVo> list = paymentService.queryList(bo);
        ExcelUtil.exportExcel(list, "ERP付款单", ErpFinancePaymentVo.class, response);
    }

    /**
     * 获取ERP付款单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erp:payment:view")
    @GetMapping("/{id}")
    public R<ErpFinancePaymentVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(paymentService.queryById(id));
    }

    /**
     * 新增ERP付款单
     */
    @SaCheckPermission("erp:payment:add")
    @Log(title = "ERP付款单", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody List<ErpFinancePaymentBo> bos) {
        return toAjax(paymentService.insertByBo(bos));
    }

    /**
     * 修改ERP付款单
     */
    @SaCheckPermission("erp:payment:update")
    @Log(title = "ERP付款单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody List<ErpFinancePaymentBo> bos) {
        return toAjax(paymentService.updateByBo(bos));
    }

    /**
     * 删除ERP付款单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erp:payment:remove")
    @Log(title = "ERP付款单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(paymentService.deleteWithValidByIds(List.of(ids)));
    }

    /**
     * 审核付款单
     *
     * @param id 付款单ID
     */
    @Log(title = "ERP付款单审核", businessType = BusinessType.UPDATE)
    @PostMapping("/audit/{id}")
    public R<Void> audit(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return toAjax(paymentService.auditPayment(id));
    }

    /**
     * 撤销审核付款单
     *
     * @param id 付款单ID
     */
    @Log(title = "ERP付款单撤销审核", businessType = BusinessType.UPDATE)
    @PostMapping("/unaudit/{id}")
    public R<Void> unaudit(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return toAjax(paymentService.unauditPayment(id));
    }

    /**
     * 获取付款单统计信息
     */
    @GetMapping("/statistics")
    public R<ErpFinancePaymentVo> getStatistics() {
        return R.ok(paymentService.getPaymentStatistics());
    }

    /**
     * 根据供应商ID获取付款记录
     *
     * @param supplierId 供应商ID
     */
    @GetMapping("/supplier/{supplierId}")
    public R<List<ErpFinancePaymentVo>> getBySupplier(@NotNull(message = "供应商ID不能为空") @PathVariable Long supplierId) {
        return R.ok(paymentService.getPaymentsBySupplier(supplierId));
    }
}
